﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SponsorshipManager.Database;

namespace SponsorshipManager.BusinessLayer
{
    public class Quota : IDbBasedObject
    {

        public int QuotaId { get; set; }
        public decimal Payment {get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

        public bool NeedsToSave
        {
            get ;
            set ;
        }

        public bool Load()
        {
            StoredProcedure sp = Database.Database.CurrentDB.StoredProcedure();
            sp.Name = "uspGetQuota";
            sp.AddParameter("QuotaId", DbType.Int32, QuotaId);
            sp.Token = Application.Token;
            int ret = sp.Execute();
            if (ret == 1) LoadDataRow(sp.QueryResult.Rows[0]);
            return (ret == 1);
        }

        public bool Save()
        {
            StoredProcedure sp = Application.Database.StoredProcedure();
            if (QuotaId > 0)
            {
                sp.Name = "uspAddQuota";
                sp.AddParameter("QuotaId", DbType.Int32, QuotaId,ParameterDirection.Output);
            }
            else
            {
                sp.Name = "uspUpdateQuota";
                sp.AddParameter("QuotaId", DbType.Int32, QuotaId);
            }
            sp.AddParameter("Payment", DbType.Decimal, Payment);
            sp.AddParameter("StartDate", DbType.Date, StartDate);
            sp.AddParameter("EndDate", DbType.Date, EndDate);
            
            sp.Token = Application.Token;
            int ret = sp.ExecuteNoQuery();
            return (ret == 1);
        }

        private bool LoadDataRow(DataRow dr)
        {
            QuotaId = (int)dr["quotaId"];
            Payment = (Decimal)dr["Payment"];
            StartDate = (DateTime)dr["startDate"];
            EndDate = (DateTime)dr["EndDate"];
            return true;
        }

    }
}
